package com.onjob.recruit;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import util.dao.Pagination;

import com.onjob.administer.City;
import com.onjob.administer.CityDao;
import com.onjob.administer.Status;
import com.onjob.administer.StatusDao;
import com.onjob.security.Users;
import com.opensymphony.xwork2.ActionSupport;

public class RecruitAction extends ActionSupport {

	/**
	 * 与公司招聘相关的action
	 */
	private static final long serialVersionUID = 1L;
	private Recruit recruit;
	private RecruitDao recruitDao;
	private CompanyDao companyDao;
	private CityDao cityDao;
	private StatusDao statusDao;
	private List<City> cityList;
	private List<Status> statusList;
	private String id;
	private Pagination<Recruit> pagination;
	private String cityName;
	private List<Recruit> resultsByCityName;

	public List<Recruit> getResultsByCityName() {
		return resultsByCityName;
	}

	public void setResultsByCityName(List<Recruit> resultsByCityName) {
		this.resultsByCityName = resultsByCityName;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public Pagination<Recruit> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Recruit> pagination) {
		this.pagination = pagination;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}

	public List<Status> getStatusList() {
		return statusList;
	}

	public void setStatusList(List<Status> statusList) {
		this.statusList = statusList;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public StatusDao getStatusDao() {
		return statusDao;
	}

	public void setStatusDao(StatusDao statusDao) {
		this.statusDao = statusDao;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public RecruitDao getRecruitDao() {
		return recruitDao;
	}

	public void setRecruitDao(RecruitDao recruitDao) {
		this.recruitDao = recruitDao;
	}

	public Recruit getRecruit() {
		return recruit;
	}

	public void setRecruit(Recruit recruit) {
		this.recruit = recruit;
	}

	public String beginRecruit() {
		cityList = cityDao.findAll();
		statusList = statusDao.findAll();
		if (id != null && !id.isEmpty()) {
			this.setRecruit(recruitDao.findById(id));
		}
		return SUCCESS;
	}

	public String editRecruit() {
		if (recruit.getId() == null || recruit.getId().isEmpty()) {
			Date date = new Date();
			recruit.setRecruitDate(date);
			 HttpServletRequest servletRequest = ServletActionContext
			 .getRequest();
			 Users user = (Users) servletRequest.getSession().getAttribute(
			 "user");
			 Company company = companyDao.getCompanyByUser(user);
//			Company company = new Company();
//			company.setId("111");
			recruit.setCompanyId(company);
		}
		recruitDao.saveOrUpdate(recruit);
		return SUCCESS;

	}

	public String deleteRecruit() {
		Recruit recruit = new Recruit();
		recruit.setId(id);
		this.recruitDao.delete(recruit);
		return SUCCESS;
	}

	public String browse() {
		if (pagination == null) {
			pagination = new Pagination<Recruit>();
		}
		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}
		this.pagination.setOrderFieldName("recruitDate");
		this.pagination.setDesc(true);
		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		this.recruitDao.browse(pagination);
		return SUCCESS;
	}

	public String findByCityName() {
		this.resultsByCityName = (List<Recruit>) this.recruitDao
				.findByCity(this.cityName);
		return SUCCESS;
	}
	
	public String recruitDetail(){
		this.recruit = this.recruitDao.findById(id);
		return SUCCESS;
	}

	public String findByCompany(){
		 HttpServletRequest servletRequest = ServletActionContext
		 .getRequest();
		 Users user = (Users) servletRequest.getSession().getAttribute(
		 "user");
		 Company company = companyDao.getCompanyByUser(user);
//		Company company = new Company();
//		company.setId("111");
		this.resultsByCityName = (List<Recruit>)this.recruitDao.findByCompany(company.getId());
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+resultsByCityName);
		return SUCCESS;
	}
}
